<template>
  <div class='container'>
<el-card>
        <el-form :inline="true">
          <div>
            <el-form-item label="学科名称">
              <el-input
                size="mini"
                style="width: 180px; margin-right: 10px"
                v-model="form.subjectName"
              ></el-input>
              <el-button size="mini" @click="clear">清除</el-button>
              <el-button size="mini" type="primary" @click="search">搜索</el-button
              >
            </el-form-item>
            <div class="rightbutton">
              <el-button
              @click="addDialog"
                size="mini"
                type="success"
                icon="el-icon-edit">
                新增学科
              </el-button>
            </div>
          </div>
          <!-- 数据总计 -->
          <el-alert
          :title="titles"
            type="info"
            class="alert"
            show-icon
          ></el-alert>
        </el-form>

            <!-- 表格区域 -->
        <el-table  style="width: 100%" :data="listFrom">
          <el-table-column label="序号" type="index" :index="indexMethod" > </el-table-column>
          <el-table-column  label="学科名称" prop="subjectName">
          </el-table-column>
          <el-table-column  label="创建者" prop="username"> </el-table-column>
          <el-table-column  label="创建日期" width="180" prop="addDate">
            <template #default="{row}">
                  {{formateTime(row.addDate)}}
            </template>
          </el-table-column>
          <el-table-column
          :formatter="formatterFn"
            prop="isFrontDisplay"
            label=" 前台是否显示" >
          </el-table-column>
          <el-table-column  label="二级目录" prop="twoLevelDirectory">
          </el-table-column>
          <el-table-column  label="标签" prop="tags"> </el-table-column>
          <el-table-column  label="题目数量" prop="totals"> </el-table-column>
          <el-table-column label="操作" width="250px">
            <template #default="{row}">
              <el-button type="text" size="small" @click="classify(row.id,row.subjectName)" >学科分类</el-button>
              <el-button type="text" size="small" @click="lables(row.id,row.subjectName)">学科标签</el-button>
              <el-button type="text" size="small" @click="editRow(row)">修改</el-button>
              <el-button type="text" size="small" @click="delRow(row)" >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
          <el-pagination
       style="margin-top: 10px"
       background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.page"
      :page-sizes="[5, 10, 15, 20]"
       :page-size="form.pagesize"
      layout="->,  prev, pager, next,sizes, jumper"
      :total="total">
    </el-pagination>
</el-card>

<!-- 新增学科 -->
<el-dialog width="450px" :title="showTitle" :visible="showDialog" @close="showDialog=false" :close-on-click-modal="false">
          <el-form
            label-width="120px"
            :rules="rules"
            :model="formAdd"
            ref="addForm"
          >
            <el-form-item label="学科名称" prop="subjectName">
              <el-input
              v-model="formAdd.subjectName"
                style="width: 50%"
                placeholder="请输入学科名称"
              />
            </el-form-item>
            <el-form-item label="是否显示">
              <el-switch
                v-model="formAdd.isFrontDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="showDialog=false" >取消</el-button>
              <el-button size="small" type="primary" @click="confirmAdd">确定</el-button>
            </el-form-item>
          </el-form>
</el-dialog>
  </div>
</template>

<script>
import { list, add, detail, update, remove } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      listFrom: [],
      form: {
        page: 1,
        pagesize: 10,
        subjectName: null,
        state: null
      },
      formAdd: {
        subjectName: '',
        isFrontDisplay: 0
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      total: 0,
      titles: '',
      showDialog: false,
      showTitle: '',
      isFrontDisplay: false,

      hireType: [
        {
          id: 1,
          value: '是'
        },
        {
          id: 0,
          value: '否'
        }
      ]
    }
  },

  created () {
    this.getList()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(val)
      this.form.pagesize = val
      this.form.page = 1
      this.getList()
    },
    // 页面发生变化时触发该事件
    handleCurrentChange (val) {
      // console.log(val)
      this.form.page = val
      this.getList()
    },
    indexMethod (index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1
    },
    async getList () {
      const res = await list({ page: this.form.page, pagesize: this.form.pagesize })
      this.form.page = +res.data.page
      this.form.pagesize = +res.data.pagesize
      this.listFrom = res.data.items
      this.total = res.data.counts
      this.titles = `一共${this.total}条数据`
    },
    // 清除输入框
    clear () {
      this.form.subjectName = ''
      this.getList()
    },
    // 搜索
    async search () {
      const { data } = await list({ subjectName: this.form.subjectName })
      this.listFrom = data.items
      this.total = data.counts
      this.form.page = +data.page
      this.form.pagesize = +data.pagesize
      this.form.page = 1
      this.titles = `数据一共${this.total}条`
    },
    // 处理时间
    formateTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    formatterFn (row, column, cellValue, index) {
      const hireType = this.hireType
      const obj = hireType.find(item => item.id === Number(cellValue))
      return obj ? obj.value : '未知'
    },
    addDialog () {
      this.showTitle = '新增学科'
      this.showDialog = true
    },
    confirmAdd () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.formAdd.id) {
          await update(this.formAdd)
          this.$message.success('修改成功')
        } else {
          await add(this.formAdd)
          this.$message.success('添加成功')
        }
        this.clearAdd()
        this.showDialog = false
        this.getList()
      })
    },
    async  editRow (data) {
      if (this.formAdd.isFrontDisplay) {
        this.formAdd.isFrontDisplay = data.isFrontDisplay
      }

      this.showTitle = '修改学科'
      this.showDialog = true
      const res = await detail(data)
      // console.log(res)
      this.formAdd = res.data
    },
    clearAdd () {
      this.formAdd = {
        subjectName: '',
        isFrontDisplay: null
      }
      this.$refs.addForm.resetFields()
    },
    delRow (id) {
      this.$confirm('你确定要删除吗', '提示').then(async () => {
        await remove(id)
        this.$message.success('删除成功')
        if (this.listFrom.length === 1 && this.form.page > 1) {
          this.form.page--
        }
        this.getList()
      }).catch(() => {})
    },
    classify (myId, myName) {
      this.$router.push(`/subjects/directorys?id=${myId}&name=${myName}`)
    },
    lables (myId, myName) {
      this.$router.push(`/subjects/tags?id=${myId}&name=${myName}`)
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 0 10px;
  margin: 10px 0;
}
.rightbutton{
  float: right;

}
</style>
